<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 3:32 PM
 */

namespace Admin\Model;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class AdminTopicImgTable{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $select = new Select('topic_img');
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminTopicImg());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function getTopicImg($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        return $row;
    }

    public function getTopicImgByTopic($topic_id)
    {
        $topic_id  = (int) $topic_id;
        $rowset = $this->tableGateway->select(array('topic_id' => $topic_id));
        return $rowset;
    }


    public function saveTopicImg(AdminTopicImg $topicImg)
    {
        $data = array(

            'link' => $topicImg->link,
            'topic_id' => $topicImg->topic_id,
            'user_id' => $topicImg->user_id,
            'img_desc' => $topicImg->img_desc

        );

        $id = (int)$topicImg->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
            return $this->tableGateway->getLastInsertValue();
        } else {
            if ($this->getTopicImg($id)) {
                $this->tableGateway->update($data, array('id' => $id));
                return $id;
            } else {
                return false;
            }
        }
        return false;

    }

    public function deleteTopicImg($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }

    public function deleteTopicImgByTopic($topic_id)
    {
        $str = 'DELETE FROM topic_img WHERE topic_id = '.$topic_id;

        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);
        return 1;
    }

    public function getTopicImgByTopicForApi($topic_id)
    {
        $str = 'SELECT * FROM topic_img WHERE topic_id = '.$topic_id;

        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);

        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }
}
